#include "mainwindow.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    //创建柱形数据集
    QBarSet* set0=new QBarSet("军方开支A");
    QBarSet*set1=new QBarSet("军方开支B");

    // 添加数据到数据集
    *set0 << 1000 << 1200 << 1500 << 2000 << 2300;
    *set1 << 1200 << 1500 << 2000 << 1500 << 3000;
    //创建柱形图序列并添加到数据集

    QBarSeries*series=new QBarSeries();
    series->append(set0);
    series->append(set1);

    //创建图表并添加序列

    QChart*chart=new QChart();
    chart->addSeries(series);
    chart->setTitle("军费开支");
    //chart->setAnimationOptions(QChart::SeriesAnimations);//这行代码用于为 Qt Charts 图表设置动画效果，让数据系列的显示和更新过程更加生动和流畅。

    // QValueAxis* axisX=new QValueAxis();
    // axisX->setRange(2018,2022);
    // axisX->setTitleText("年份");//连续的数字数据，用于折线图数值轴会将2018、2019、2020等作为数值处理，导致柱形位置计算错误
    QStringList categories;
    categories << "2018" << "2019" << "2020" << "2021" << "2022";
    QBarCategoryAxis* axisX=new QBarCategoryAxis();
    axisX->append(categories);  // 添加分类标签


    QValueAxis* axisY=new QValueAxis();
    axisY->setRange(0,4000);
    axisY->setTitleText("开支（单位：万亿）");
    chart->addAxis(axisX, Qt::AlignBottom);
    chart->addAxis(axisY, Qt::AlignLeft);
    chart->legend()->setVisible(false);//显示图例
    chart->legend()->setAlignment(Qt::AlignBottom);
     series->attachAxis(axisX);
    series->attachAxis(axisY);
    //创建视图
    QChartView *view=new QChartView(chart);
    // view->setRenderHint(QPainter::Antialiasing);//Qt Charts 图表的抗锯齿（Antialiasing）渲染，显著提升图表的视觉质量用于消除图形边缘的锯齿状像素阶梯，使曲线和斜线看起来更加平滑自然。

    MainWindow w;
    w.setCentralWidget(view);
    w.resize(800,600);

    w.show();
    return a.exec();
}
